﻿using System;
using System.Web.UI;

namespace AjaxControlToolkitCustom
{
    [RequiredScript(typeof(AnimationScripts)), RequiredScript(typeof(CommonToolkitScripts)), ClientCssResource("AjaxControlToolkit.UploadControl.MattBerseth.WebControls.AJAX.Progress._assets.css.progress.css"), ClientScriptResource("AjaxControlToolkit.ProgressBehavior", "AjaxControlToolkit.UploadControl.MattBerseth.WebControls.AJAX.Progress.ProgressBehavior.js")]
    public class ProgressControl : ScriptControlBase
    {
        protected override void AddAttributesToRender(HtmlTextWriter writer)
        {
            base.Style.Remove(HtmlTextWriterStyle.Width);
            writer.AddAttribute(HtmlTextWriterAttribute.Class, string.Format("ajax__progress {0}", this.CssClass).TrimEnd(new char[0]));
            base.AddAttributesToRender(writer);
        }

        protected V GetPropertyValue<V>(string propertyName, V nullValue)
        {
            if (this.ViewState[propertyName] == null)
            {
                return nullValue;
            }
            return (V)this.ViewState[propertyName];
        }

        public override void RenderBeginTag(HtmlTextWriter writer)
        {
            writer.AddAttribute(HtmlTextWriterAttribute.Class, string.Format("ajax__progress {0}", this.CssClass).TrimEnd(new char[0]));
            base.RenderBeginTag(writer);
        }

        protected override void RenderContents(HtmlTextWriter writer)
        {
            writer.AddAttribute(HtmlTextWriterAttribute.Class, "ajax__progress_outer");
            writer.AddAttribute(HtmlTextWriterAttribute.Id, this.ClientID + "_outer");
            writer.AddStyleAttribute(HtmlTextWriterStyle.Width, this.Width.ToString());
            writer.RenderBeginTag(HtmlTextWriterTag.Div);
            writer.AddAttribute(HtmlTextWriterAttribute.Class, "ajax__progress_inner");
            writer.AddAttribute(HtmlTextWriterAttribute.Id, this.ClientID + "_inner");
            writer.RenderBeginTag(HtmlTextWriterTag.Div);
            writer.AddAttribute(HtmlTextWriterAttribute.Class, "ajax__progress_indicator");
            writer.AddAttribute(HtmlTextWriterAttribute.Id, this.ClientID + "_indicator");
            writer.RenderBeginTag(HtmlTextWriterTag.Div);
            writer.RenderEndTag();
            writer.RenderEndTag();
            writer.RenderEndTag();
            writer.AddAttribute(HtmlTextWriterAttribute.Class, "ajax__progress_info");
            writer.AddAttribute(HtmlTextWriterAttribute.Id, this.ClientID + "_info");
            writer.RenderBeginTag(HtmlTextWriterTag.Div);
            writer.RenderEndTag();
        }

        protected void SetPropertyValue<V>(string propertyName, V value)
        {
            this.ViewState[propertyName] = value;
        }

        [ExtenderControlProperty]
        public bool Animate
        {
            get
            {
                return this.GetPropertyValue<bool>("Animate", true);
            }
            set
            {
                this.SetPropertyValue<bool>("Animate", value);
            }
        }

        [RequiredProperty, ExtenderControlProperty]
        public Mode Mode
        {
            get
            {
                return this.GetPropertyValue<Mode>("Mode", Mode.Manual);
            }
            set
            {
                this.SetPropertyValue<Mode>("Mode", value);
            }
        }

        [ExtenderControlProperty]
        public bool ShowStatusText
        {
            get
            {
                return this.GetPropertyValue<bool>("ShowStatusText", true);
            }
            set
            {
                this.SetPropertyValue<bool>("ShowStatusText", value);
            }
        }

        [ExtenderControlProperty]
        public float Speed
        {
            get
            {
                return this.GetPropertyValue<float>("Speed", 0f);
            }
            set
            {
                this.SetPropertyValue<float>("Speed", value);
            }
        }

        [ExtenderControlProperty]
        public string StatusText
        {
            get
            {
                return this.GetPropertyValue<string>("StatusText", "{0}%");
            }
            set
            {
                this.SetPropertyValue<string>("StatusText", value);
            }
        }

        protected override HtmlTextWriterTag TagKey
        {
            get
            {
                return HtmlTextWriterTag.Div;
            }
        }

        [ExtenderControlProperty]
        public override string ToolTip
        {
            get
            {
                return this.GetPropertyValue<string>("TooltipText", "{0}%");
            }
            set
            {
                this.SetPropertyValue<string>("TooltipText", value);
            }
        }

        [ExtenderControlProperty]
        public int Value
        {
            get
            {
                return this.GetPropertyValue<int>("Value", 0);
            }
            set
            {
                this.SetPropertyValue<int>("Value", value);
            }
        }
    }
}

